﻿using Nancy;
using API_Service.BizLogic.Admin;

namespace API_Service.Module
{
    public class AdminModule : BaseAPIModule
    {
        private AdminService adminService = new AdminService();

        public AdminModule() : base("/admin")
        {
            Get["/"] = _ =>
            {
                return Response.AsJson("系统管理模块");
            };

            #region 用户管理

            Get["GetUserRole","/user/role/{userid}"] = p =>
            {
                return Response.AsJson(adminService.GetUserRoleByID((string)p.userid));
            };

            Get["/user"] = _ =>
            {
                return Response.AsJson(adminService.GetUserList());
            };

            Get["/user/{uid}"] = p =>
            {
                return Response.AsJson(adminService.GetUser((string)p.uid));
            };

            Post["AddUser", "/user/add", (ctx) => ctx.Request.Form.MODEL] = _ =>
            {
                string result = Request.Form["MODEL"];

                return Response.AsJson(adminService.AddUser(result));
            };

            Post["/user/delete/{userid}"] = p =>
            {
                return Response.AsJson(adminService.DeleteUser((string)p.userid));
            };

            Post["/user/update"] = _ =>
            {
                string result = Request.Form["MODEL"];

                return Response.AsJson(adminService.UpdateUser(result));
            };

            #endregion

            #region 角色管理

            Get["/role"] = _ =>
            {
                return Response.AsJson(adminService.GetRoleList());
            };

            Get["/role/{rid}"] = p =>
            {
                return Response.AsJson(adminService.GetRole((string)p.rid));
            };

            Get["/role/menu/{rid}"] = p =>
            {
                return Response.AsJson(adminService.GetRoleMenu((string)p.rid));
            };

            Post["/role/add"] = _ =>
            {
                string result = Request.Form["MODEL"];

                return Response.AsJson(adminService.AddRole(result));
            };

            Post["/role/update"] = _ =>
            {
                string result = Request.Form["MODEL"];

                return Response.AsJson(adminService.UpdateRole(result));
            };

            Post["/role/delete/{rid}"] = p =>
            {
                return Response.AsJson(adminService.DeleteRole((string)p.rid));
            };

            #endregion

            #region 菜单管理

            Get["/menu"] = _ =>
            {
                return Response.AsJson(adminService.GetMenuList());
            };

            Get["/menu/{menuid}"] = p =>
            {
                return Response.AsJson(adminService.GetMenu((string)p.menuid));
            };

            Post["/menu/save"] = _ =>
            {
                string result = Request.Form["MODEL"];

                return Response.AsJson(adminService.SaveMenu(result));
            };

            Post["/menu/delete/{menuid}"] = p =>
            {
                string result = Request.Form["MODEL"];

                return Response.AsJson(adminService.DeleteMenu((string)p.menuid));
            };

            #endregion

        }
    }
}